package com.example.interviewrecorder.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.example.interviewrecorder.LyyApplicaiton
import com.example.interviewrecorder.LyyBaseActivity
import com.example.interviewrecorder.LyyDatabase
import com.example.interviewrecorder.R
import com.example.interviewrecorder.components.showTip
import com.example.interviewrecorder.dao.SecurityQuestionDao
import com.example.interviewrecorder.databinding.ActivitySecurityQuestionBinding
import com.example.interviewrecorder.utils.LyyActivityCollector
import com.example.interviewrecorder.utils.LyyLogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class SecurityQuestionActivity : LyyBaseActivity(), View.OnClickListener {
    private val mBinding: ActivitySecurityQuestionBinding by lazy {
        ActivitySecurityQuestionBinding.inflate(layoutInflater)
    }

    private val securityQuesitonDao: SecurityQuestionDao =
        LyyDatabase.getDatabase().getSecurityQuestionDao()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        initEvents()
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    private fun initData() {
        lifecycleScope.launch {
            launch(Dispatchers.IO) {
                val currUser = LyyApplicaiton.currUser
                currUser?.let {
                    runCatching {
                        val list = securityQuesitonDao.querySQByUserId(it.id)
                        runOnUiThread {
                            val securityQuesitonOne = list[0]
                            val securityQuesitonTwo = list[1]
                            val securityQuesitonThree = list[2]

                            mBinding.questionOneRight.setText(securityQuesitonOne.quesiton)
                            mBinding.answerOneRight.setText(securityQuesitonOne.answer)
                            mBinding.questionTwoRight.setText(securityQuesitonTwo.quesiton)
                            mBinding.answerTwoRight.setText(securityQuesitonTwo.answer)
                            mBinding.questionThreeRight.setText(securityQuesitonThree.quesiton)
                            mBinding.answerThreeRight.setText(securityQuesitonThree.answer)

                            if (securityQuesitonOne.quesiton.isBlank() && securityQuesitonOne.answer.isBlank() && securityQuesitonTwo.quesiton.isBlank() && securityQuesitonTwo.answer.isBlank() && securityQuesitonThree.quesiton.isBlank() && securityQuesitonThree.answer.isBlank()) {
                                showTip(content = getString(R.string.not_set_security_question_tip))
                            }
                        }
                    }.onFailure {
                        LyyLogUtil.e(TAG, "initData() fail ...")
                    }

                }
            }
        }
    }

    private fun initEvents() {
        mBinding.editBtn.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.editBtn -> {
                EditSecurityQuestionActivity.start()
            }
        }
    }

    companion object {
        private const val TAG = "SecurityQuestionActivit"
        fun start() {
            val currAct = LyyActivityCollector.getCurrActivity()
            val intent = Intent(currAct, SecurityQuestionActivity::class.java)
            currAct.startActivity(intent)
        }
    }
}